/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: GoldenUserDetailService
 * Author:   zhouheng
 * Date:     2019/6/8 22:09
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zhouh.golden.security.service;

import com.zhouh.golden.common.utils.DateUtil;
import com.zhouh.golden.security.domain.GoldenSocialDetails;
import com.zhouh.golden.system.domain.MyUser;
import com.zhouh.golden.system.service.MenuService;
import com.zhouh.golden.security.domain.GoldenUserDetails;
import com.zhouh.golden.security.domain.LoginType;
import com.zhouh.golden.security.exception.GoldenCredentialExcetion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.zhouh.golden.system.service.UserService;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/6/8
 * @since 1.0.0
 */
@Slf4j
@Component
public class GoldenUserDetailService implements UserDetailsService,SocialUserDetailsService {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("表单登录用户名:" + username);

        MyUser user = this.userService.findByNameOrMobile(username);
        boolean isMobile = isPhoneNo(username);
        if (user != null) {
            String permissions = this.menuService.findUserPermissions(user.getUsername());
            boolean notLocked = false;
            if (StringUtils.equals(MyUser.STATUS_VALID, user.getStatus())){
                notLocked = true;
            }
            GoldenUserDetails userDetails = new GoldenUserDetails(user.getUsername(), user.getPassword(), true, true, true, notLocked,
                    AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));
            userDetails.setTheme(user.getTheme());
            userDetails.setAvatar(user.getAvatar());
            userDetails.setEmail(user.getEmail());
            userDetails.setMobile(user.getMobile());
            userDetails.setSsex(user.getSsex());
            userDetails.setUserId(user.getUserId());
            userDetails.setPassword(user.getPassword());
            userDetails.setLoginTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_DATE_FORMAT));
            if (isMobile) {
                userDetails.setLoginType(LoginType.sms);
            }
            return userDetails;
        }else {
            if (isMobile) {
                throw new GoldenCredentialExcetion("该手机号还未绑定账号！");
            }

            throw new UsernameNotFoundException("用户名不存在");
        }
    }

    /**
     * @description: 是否手机号登陆
     * @date: 2019/6/9 9:05
     * @author:zhouheng
     */
    public static boolean isPhoneNo(String phoneNo) {
        String regex = "[1]\\d{10}";
        if (StringUtils.isBlank(phoneNo)){
            return false;
        }else {
            return phoneNo.matches(regex);
        }
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        MyUser user = this.userService.findByNameOrMobile(userId);
        if (user != null) {
            String permissions = this.menuService.findUserPermissions(user.getUsername());
            boolean notLocked = false;
            if (StringUtils.equals(MyUser.STATUS_VALID, user.getStatus()))
                notLocked = true;
            GoldenSocialDetails userDetails = new GoldenSocialDetails(user.getUsername(), user.getPassword(), true, true, true, notLocked,
                    AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));
            userDetails.setTheme(user.getTheme());
            userDetails.setAvatar(user.getAvatar());
            userDetails.setEmail(user.getEmail());
            userDetails.setMobile(user.getMobile());
            userDetails.setSsex(user.getSsex());
            userDetails.setUsersId(user.getUserId());
            userDetails.setPassword(user.getPassword());
            userDetails.setLoginTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_DATE_FORMAT));
            return userDetails;
        } else {
            return null;
        }
    }
}